//
//  JiaochengViewController.m
//  AStage_shougongke
//
//  Created by 赵磊 on 16/1/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "aStageHeader.pch"
@interface JiaochengViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
// 热门课程数据数组
@property (nonatomic, retain) NSArray *dataArr;
// 首页collectionView
@property (nonatomic, retain) UICollectionView *kechengCollectionView;
@property (nonatomic, retain) NSArray *lunboArr;
// 教程分类数组数组
@property (nonatomic, retain) NSArray *jiaochengArr;
// 手工专题数据数组
@property (nonatomic, retain) NSArray *shougongArr;
// 向header上面添加的view
@property (nonatomic, retain) UICollectionReusableView *headerView;
@property (nonatomic, retain) UICollectionReusableView *footerView;
@property (nonatomic, assign) NSInteger i;
@property (nonatomic, assign) NSInteger n;
@end

@implementation JiaochengViewController
- (void)dealloc
{
    [_dataArr release];
    [_kechengCollectionView release];
    [_jiaochengArr release];
    [_shougongArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createView];
    self.n = 0;
    self.i = 0;
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:234 / 255.0 green:86 / 255.0 blue:81 / 255.0 alpha:1.0];
    self.navigationItem.title = @"教程";
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    // 添加数据处理方法
    [self dataHandler];
    // 在页面中添加collectionView
    // 定义collectionView的layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((WIDTH - 30) / 2, (WIDTH - 30) / 2 / 0.618);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.headerReferenceSize = CGSizeMake(100, self.view.frame.size.height);
    layout.footerReferenceSize = CGSizeMake(100, 80);
    self.kechengCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    _kechengCollectionView.backgroundColor = [UIColor whiteColor];
    _kechengCollectionView.delegate = self;
    _kechengCollectionView.dataSource = self;
    // 注册collectionView的cell重用池
    [_kechengCollectionView registerClass:[JiaochengCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    // 给collectionView添加一个header
    [_kechengCollectionView registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    // 给collectionView添加一个footer
    [_kechengCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    [self.view addSubview:_kechengCollectionView];
    // 下拉刷新
    self.kechengCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(dataHandler)];
    // 判断是否首次进入程序 不进行刷新
    if (_jiaochengArr.count != 0) {
        [self.kechengCollectionView.mj_header beginRefreshing];
        [self.kechengCollectionView.mj_header endRefreshing];
    }
    [_kechengCollectionView release];
}
// 创建一个headerView
- (void)createView
{
    UIScrollView *lunbo = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 4)];
    lunbo.backgroundColor = [UIColor redColor];
    lunbo.pagingEnabled = YES;
    lunbo.contentSize = CGSizeMake(WIDTH * _lunboArr.count, 0);
    for (NSInteger i = 0; i < _lunboArr.count; i++) {
        LunboModel *m = self.lunboArr[i];
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT / 4)];
        [imgView sd_setImageWithURL:[NSURL URLWithString:m.host_pic]];
        [lunbo addSubview:imgView];
        [imgView release];
    }
    [_headerView addSubview:lunbo];
    UILabel *fenlei = [[UILabel alloc] initWithFrame:CGRectMake(10, HEIGHT / 4, 100, HEIGHT / 4 / 3)];
    fenlei.text = @"教程分类";
    [_headerView addSubview:fenlei];
        // 添加Button
        for (NSInteger i = 0; i < 8; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            // 两行button 判断其铺设位置
            if (i < 4)
            {
                // 设置各个button的位置
                button.frame = CGRectMake(1 * (i + 1) + (WIDTH - 5) / 4 * i, HEIGHT / 4 + HEIGHT / 4 / 3, (WIDTH - 5) / 4, (WIDTH - 5) / 4);
                button.backgroundColor = [UIColor colorWithRed:244 / 255.0 green:244 / 255.0 blue:244 / 255.0 alpha:1.0];
                button.tag = 1000 + [[_jiaochengArr[i] cate_id] floatValue];
                // 是指分类的图标
                UIImageView *photo = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, (WIDTH - 5) / 4 / 3 * 1.5, (WIDTH - 5) / 4 / 3 * 1.5)];
                photo.center = CGPointMake((WIDTH - 5) / 4 / 2, (WIDTH - 5) / 12);
                [photo sd_setImageWithURL:[NSURL URLWithString:[_jiaochengArr[i] cate_pic]]];
                // 设置分类名的label
                UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, (WIDTH - 5) / 4, 26)];
                name.textAlignment = NSTextAlignmentCenter;
                name.center = CGPointMake((WIDTH - 5) / 4 / 2, (WIDTH - 5) /  6);
                name.text = [_jiaochengArr[i] cate_name];
                // 给button添加触发方法
                [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
                // 往button上面添加其对应的名称与图标
                [button addSubview:photo];
                [button addSubview:name];
                    [_headerView addSubview:button];
                    [photo release];
                    [name release];
                } else {
                    // 设置下一行的4个button
                    button.frame = CGRectMake(1 * (i + 1 - 4) + (WIDTH - 5) / 4 * (i - 4), HEIGHT / 4 + HEIGHT / 4 / 3 + (WIDTH - 5) / 4 + 1, (WIDTH - 5) / 4, (WIDTH - 5) / 4);
                    button.backgroundColor = [UIColor colorWithRed:244 / 255.0 green:244 / 255.0 blue:244 / 255.0 alpha:1.0];
                    // 设置分类名的label
                    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, (WIDTH - 5) / 4, 26)];
                    name.textAlignment = NSTextAlignmentCenter;
                    name.center = CGPointMake((WIDTH - 5) / 4 / 2, (WIDTH - 5) /  6);
                    //            name.backgroundColor = [UIColor redColor];
                    name.text = [_jiaochengArr[i] cate_name];
                    UIImageView *photo = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, (WIDTH - 5) / 4 / 3 * 1.5, (WIDTH - 5) / 4 / 3 * 1.5)];
                    photo.center = CGPointMake((WIDTH - 5) / 4 / 2, (WIDTH - 5) / 12);
                    [photo sd_setImageWithURL:[NSURL URLWithString:[_jiaochengArr[i] cate_pic]]];
                    button.tag = 1000 + [[_jiaochengArr[i] cate_id] floatValue];
                    // 更多分类为特殊网址
                    if (i == 7) {
                        button.tag = 1000 + 8;
                    }
                    // 给button添加触发方法
                    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
                    // 往button上面添加其对应的名称与图标
                    [button addSubview:photo];
                    [button addSubview:name];
                    [_headerView addSubview:button];
                    [photo release];
                    [name release];
                }
            }
//     添加一个分隔条
            UIView *fenge = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT / 2 + HEIGHT / 4 / 3 + 31, WIDTH, 13)];
            fenge.backgroundColor = [UIColor colorWithRed:244 / 255.0 green:244 / 255.0 blue:244 / 255.0 alpha:1.0];
            // 添加手工专题button
            UIButton *zhuantiButton = [UIButton buttonWithType:UIButtonTypeCustom];
            zhuantiButton.frame = CGRectMake(0, HEIGHT / 2 + HEIGHT / 4 / 3 + 54, WIDTH, HEIGHT / 4);
            // 给专题添加触发方法
            [zhuantiButton addTarget:self action:@selector(zhuantiAction) forControlEvents:UIControlEventTouchUpInside];
            // 手工专题Label
            UILabel *shougong = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 300, 20)];
            shougong.text = @"手工专题";
            [zhuantiButton addSubview:shougong];
            // 手工专题题目Label
            UILabel *zhuanti = [[UILabel alloc] initWithFrame:CGRectMake(0, HEIGHT / 4 - 15, WIDTH, 15)];
            zhuanti.textAlignment = NSTextAlignmentCenter;
            zhuanti.font = [UIFont systemFontOfSize:13];
            zhuanti.text = [_shougongArr[0] subject];
            [zhuantiButton addSubview:zhuanti];
            // 手工专题图片
            // 高度 = 手工专题高度 - 上下Label高度 - 10 (边距)
            // 宽度 = 手工专题宽度 - 左右边距宽度 - 1
            // 左边最大图片
            UIImageView *bigImg = [[UIImageView alloc] initWithFrame:CGRectMake((WIDTH - (HEIGHT / 4 - 45) * 2 - 40) / 2, 25, HEIGHT / 4 - 45, HEIGHT / 4 - 45)];
            [bigImg sd_setImageWithURL:[NSURL URLWithString:[_shougongArr[0] pic][0]]];
            [zhuantiButton addSubview:bigImg];
            //左上小图片
            UIImageView *liftUpImg = [[UIImageView alloc] initWithFrame:CGRectMake((WIDTH - (HEIGHT / 4 - 45) * 2 - 40) / 2 + 40 + HEIGHT / 4 - 45, 25, (HEIGHT / 4 - 45) / 2, (HEIGHT / 4 - 45) / 2)];
            [liftUpImg sd_setImageWithURL:[NSURL URLWithString:[_shougongArr[0] pic][1]]];
            [zhuantiButton addSubview:liftUpImg];
            // 右上小图片
            UIImageView *rightUpImg = [[UIImageView alloc] initWithFrame:CGRectMake((WIDTH - (HEIGHT / 4 - 45) * 2 - 40) / 2 + 40 + HEIGHT / 4 - 45 + (HEIGHT / 4 - 45) / 2 + 1, 25, (HEIGHT / 4 - 45) / 2, (HEIGHT / 4 - 45) / 2)];
            [rightUpImg sd_setImageWithURL:[NSURL URLWithString:[_shougongArr[0] pic][2]]];
            [zhuantiButton addSubview:rightUpImg];
            // 左下小图片
            UIImageView *liftDownImg = [[UIImageView alloc] initWithFrame:CGRectMake((WIDTH - (HEIGHT / 4 - 45) * 2 - 40) / 2 + 40 + HEIGHT / 4 - 45, 25 + (HEIGHT / 4 - 45) / 2 + 1, (HEIGHT / 4 - 45) / 2, (HEIGHT / 4 - 45) / 2)];
            [liftDownImg sd_setImageWithURL:[NSURL URLWithString:[_shougongArr[0] pic][3]]];
            [zhuantiButton addSubview:liftDownImg];
            UIImageView *rightDownImg = [[UIImageView alloc] initWithFrame:CGRectMake((WIDTH - (HEIGHT / 4 - 45) * 2 - 40) / 2 + 40 + HEIGHT / 4 - 45 + (HEIGHT / 4 - 45) / 2 + 1, 25 + (HEIGHT / 4 - 45) / 2 + 1, (HEIGHT / 4 - 45) / 2, (HEIGHT / 4 - 45) / 2)];
            [rightDownImg sd_setImageWithURL:[NSURL URLWithString:[_shougongArr[0] pic][4]]];
            [zhuantiButton addSubview:rightDownImg];
            // 第二个分隔条
            UIView *secondFenge = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT - HEIGHT / 4 / 3 + 10, WIDTH, 13)];
            secondFenge.backgroundColor = [UIColor colorWithRed:244 / 255.0 green:244 / 255.0 blue:244 / 255.0 alpha:1.0];
            // 热门专题Label
            UILabel *renmen = [[UILabel alloc] initWithFrame:CGRectMake(10, HEIGHT - 20, WIDTH, 20)];
            renmen.text = @"热门教程";
            // 向header中添加控件
            [_headerView addSubview:renmen];
            [_headerView addSubview:secondFenge];
            [_headerView addSubview:zhuantiButton];
            [_headerView addSubview:fenge];
            // 对控件引用计数释放
            [shougong release];
            [secondFenge release];
            [lunbo release];
            [fenlei release];
            [fenge release];
            [zhuanti release];
            [liftDownImg release];
            [liftUpImg release];
            [rightDownImg release];
            [rightUpImg release];
}
- (void)createFooter
{
    // 查看更多为一整个button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.backgroundColor = [UIColor colorWithRed:234 / 255.0 green:86 / 255.0 blue:81 / 255.0 alpha:1.0];
    button.frame = CGRectMake(0, 0, (WIDTH - 30) / 2 - 30, 40);
    [button setTitle:@"查看更多" forState:UIControlStateNormal];
    button.tintColor = [UIColor whiteColor];
    button.center = CGPointMake(_footerView.frame.size.width / 2, _footerView.frame.size.height / 2);
    // 给查看更多添加触发事件
    [button addTarget:self action:@selector(chufaAction) forControlEvents:UIControlEventTouchUpInside];
    [_footerView addSubview:button];
}
// 设置头部和尾部
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    // 判断设置头部还是尾部
    if (kind == UICollectionElementKindSectionHeader) {
        self.headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        _headerView.backgroundColor = [UIColor whiteColor];
        return _headerView;
    } else {
        // 设置collectionView尾部为查看更多
        self.footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
        if (self.n == 0) {
            [self createFooter];
            _n = 9;
        }
        return _footerView;
    }
}
// 查看更多触发方法
- (void)chufaAction
{
    CkgdVC * chakanxiangqingVC = [[CkgdVC alloc] init];
//    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:chakanxiangqingVC animated:YES];
    // 给查看更多详情页面属性传其网址
    chakanxiangqingVC.url = @"http://m.shougongke.com/index.php?&c=Course&a=newCourseList&gcate=allcate&order=new&vid=15";
    [chakanxiangqingVC release];
}
// 手工专题触发方法
- (void)zhuantiAction
{
    ZhuantiVC *zhuantiVC = [[ZhuantiVC alloc] init];
//    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:zhuantiVC animated:YES];
    [zhuantiVC release];
}
// 教程分类触发方法(利用tag来计算传送的id)
- (void)buttonAction:(UIButton *)btn
{
    JiaochengxiangqingVC *jiaochengfenleiVC = [[JiaochengxiangqingVC alloc] init];
    [self.navigationController pushViewController:jiaochengfenleiVC animated:YES];
    NSLog(@"%ld", btn.tag);
    
}
// 热门教程触发方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    JiaochengxiangqingVC * jiaochenVC = [[JiaochengxiangqingVC alloc] init];
    RenmenModel *m = _dataArr[indexPath.item];
    jiaochenVC.hand_id = m.hand_id;
    [self.navigationController pushViewController:jiaochenVC animated:YES];
}
// 数据处理方法
- (void)dataHandler
{
    [AFNModel GETWithURL:@"http://m.shougongke.com/index.php?&c=index&a=indexnew&vid=15" completion:^(id result) {
        NSArray *arrTemp = [NSArray array];
        arrTemp = result[@"data"][@"course"];
        self.dataArr = [RenmenModel modelHandlerWithArray:arrTemp];
        arrTemp = result[@"data"][@"gcate"];
        self.jiaochengArr = [FenleiModel modelHandlerWithArray:arrTemp];
        //        NSLog(@"%@", _jiaochengArr);
        arrTemp = result[@"data"][@"topic"];
        self.shougongArr = [ShougongModel modelHandlerWithArray:arrTemp];
        arrTemp = result[@"data"][@"slide"];
        self.lunboArr = [LunboModel modelHandlerWithArray:arrTemp];
        if (_i == 0) {
            [self createView];
            _i = 23;
        }
        [_kechengCollectionView reloadData];
    }];
    [self.kechengCollectionView.mj_header endRefreshing];
}
// 返回Item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}
// 返回具体Item
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    JiaochengCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    RenmenModel *m = _dataArr[indexPath.item];
    cell.bgView.backgroundColor = [UIColor changeColorWithString:m.bg_color];
    [cell.photo sd_setImageWithURL:[NSURL URLWithString:m.host_pic]];
    cell.title.text = m.subject;
    cell.writer.text = [NSString stringWithFormat:@"by %@", m.user_name];
    cell.popularity.text = [NSString stringWithFormat:@"%@人气/%@收藏", m.view, m.collect];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
